//
//  JPLoginView.m
//  JPKY
//
//  Created by Vitas on 2016/11/14.
//  Copyright © 2016年 Vitas. All rights reserved.
//

#import "JPLoginView.h"
#import "HMAdditions.h"

@interface JPLoginView ()

/* 头像 */
@property (strong, nonatomic) UIImageView *portraitImgView;

/* 注册按钮 */
@property (strong, nonatomic) UIButton *registButton;

/* 登录按钮 */
@property (strong, nonatomic) UIButton *loginButton;

@end

@implementation JPLoginView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        
        
    }
    return self;
}

/**
 初始化UI
 */
- (void)setupUI {

    self.backgroundColor = [UIColor hm_colorWithRed:87 green:199 blue:199];
    
    [self addSubview:self.portraitImgView];
    [self addSubview:self.registButton];
    [self addSubview:self.loginButton];
    
    // 设置头像图片
    self.portraitImgView.image = [UIImage imageNamed:@"head.jpeg"];
    self.portraitImgView.layer.cornerRadius = 30;
    self.portraitImgView.layer.masksToBounds = YES;
    
    NSString *registration = @"注册";
    NSString *login = @"登录";

    [self.registButton setTitle:registration forState:UIControlStateNormal];
    [self.registButton addTarget:self action:@selector(regist:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.loginButton setTitle:login forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    
    // 自动布局
    [self.portraitImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.equalTo(self).offset(20);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    [self.registButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.portraitImgView.mas_left).offset(-15);
        make.centerY.equalTo(self.portraitImgView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.portraitImgView.mas_right).offset(15);
        make.centerY.equalTo(self.portraitImgView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    
}

#pragma mark - 注册与登录响应事件

- (void)regist:(UIButton *)button {
    
    // 发送注册广播
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Regist" object:nil userInfo:nil];
}

- (void)login:(UIButton *)button {
    
    // 发送登录广播
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Login" object:nil userInfo:nil];
}


#pragma mark - 懒加载子控件

- (UIImageView *)portraitImgView {
    
    if (!_portraitImgView) {
        _portraitImgView = [[UIImageView alloc] init];
    }
    
    return _portraitImgView;
}

- (UIButton *)registButton {
    
    if (!_registButton) {
        _registButton = [[UIButton alloc] init];
        _registButton.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    
    return _registButton;
}

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [[UIButton alloc] init];
        _loginButton.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    
    return _loginButton;
}









@end
